package cn.tedu.boyou.acticle.service.impl;

import cn.tedu.boyou.acticle.domain.Travel;
import cn.tedu.boyou.acticle.domain.TravelContent;
import cn.tedu.boyou.acticle.mapper.TravelContentMapper;
import cn.tedu.boyou.acticle.mapper.TravelMapper;
import cn.tedu.boyou.acticle.service.ITravelService;
import cn.tedu.boyou.common.query.TravelQuery;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Service
public class TravelServiceImpl implements ITravelService {
    @Autowired
    private TravelMapper travelMapper;

    @Autowired
    private TravelContentMapper travelContentMapper;

    public PageInfo query(TravelQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrder());
        List<Travel> travels = travelMapper.selectForList(qo);
        return new PageInfo(travels);
    }

    public Long saveOrUpdate(Travel travel) {
        //获取游记的摘要200字
        String content = travel.getTravelContent().getContent();
        if(content.length() > 200){
            travel.setSummary(content.substring(0,200));
        }else{
            travel.setSummary(content);
        }


        //最后更新的时间
        travel.setLastUpdateTime(new Date());

        if(travel.getId() != null){
            travelMapper.updateByPrimaryKey(travel);
            TravelContent travelContent = travel.getTravelContent();
            travelContent.setId(travel.getId());
            travelContentMapper.updateByPrimaryKey(travelContent);
        }else{
            travel.setCreateTime(new Date());
            travelMapper.insert(travel);
            System.err.println(travel.getId()+"------travelId");

            TravelContent travelContent = travel.getTravelContent();
            travelContent.setId(travel.getId());
            travelContentMapper.insert(travelContent);
        }

        return travel.getId();
    }

    public void delete(Long id) {
        travelMapper.deleteByPrimaryKey(id);
    }

    public List<Travel> listAll() {
        return travelMapper.selectAll();
    }

    public List<Travel> getHotReadTop3(Long destId) {
        return travelMapper.selectHotReadTop3(destId);
    }

    public Travel get(Long tId) {
        return travelMapper.selectByPrimaryKey(tId);
    }

    public TravelContent getContent(Long id) {

        return travelContentMapper.selectByPrimaryKey(id);
    }

    public List<Travel> list() {
        return travelMapper.selectAll();
    }

    public List<Travel> getNewTravel(Long uid) {
        return travelMapper.selectNewTravel(uid);
    }

    public int getTravelConut(Long uid) {
        return travelMapper.selectCountByAuthorId(uid);
    }

    public List<Travel> listByUserId(Long uid) {
        return travelMapper.selectTravelByUserId(uid);
    }

    public PageInfo queryByUserId(TravelQuery qo, Long uid) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Travel> travels = travelMapper.selectForListByUserId(qo,uid);
        return new PageInfo(travels);
    }

}
